
Page({
  data: {
    navTab: ["问答区", "经验区", "资料区"],
    currentNavtab: "0",
  },

  // ------------------数据加载模块 start-----------
  onLoad: function () {
    this.getAllQuestion();  // 获取所有问题
    //调用应用实例的方法获取全局数据
  },
  onTabItemTap(item) {  // 切换到“论坛”，刷新
    this.getAllQuestion();
  },
  onReachBottom: function() {
    this.getAllQuestion();  // 下拉刷新
  },
  // ------------------数据加载模块 end-----------
  
  // ------------------功能模块 start-----------
  // 获取问答区数据
  getAllQuestion:function(){
    var mark = 'getAllQuestion';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data:{
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var qList = res.data.data.qList;
        this.setData({
          questionList:qList,
        })
        //console.log(this.data.questionList)
      }
    });
  },
  // 获取经验区数据
  getAllArticle:function(){
    var mark = 'getAllArticle';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data:{
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var artList = res.data.data.artList;
        this.setData({
          articleList:artList,
        })
        console.log(this.data.articleList)
      }
    });
  },
  // 获取资料区数据
  getAllResource:function(){
    var mark = 'getAllResource';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method: 'POST',
      data:{
        mark:mark,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var rsList = res.data.data.rsList;
        this.setData({
          resourceList:rsList,
        })
        console.log(this.data.resourceList)
      }
    });
  },
  // ------------------功能模块 end-----------

  // ------------------页面切换、跳转模块 start-----------
  switchTab: function(e){
    var idx = e.currentTarget.dataset.idx;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    if(idx==0){
      this.getAllQuestion(); // 切换到问答区，重新获取数据
    }else if(idx==1){
      this.getAllArticle(); // 切换到经验区，重新获取数据
    }else if(idx==2){
      this.getAllResource(); // 切换到资料区，获取/重新获取数据
    }
  },
  // 跳转到问题页面
  goQuestion: function(e) {
    console.log(e)
    var qid = e.currentTarget.dataset.qid;
    var qTitle = e.currentTarget.dataset.qtitle;
    var qContent = e.currentTarget.dataset.qcontent;
    var qFocusNum = e.currentTarget.dataset.qfocusnum;
    console.log("qid:"+qid)
    wx.navigateTo({
      url: '../question/question?qid='+qid+'&qTitle='+qTitle+'&qFocusNum='+qFocusNum+'&qContent='+qContent
    })
  },

  goExperienceDetail: function(e) {
    var article = e.currentTarget.dataset.article
    //console.log(article);
    var articleJson = JSON.stringify(article);
    wx.navigateTo({
      url: '../forum/experienceDetail/experienceDetail?article='+articleJson
    })
  },
  goInformationDetail: function(e) {
    var resource = e.currentTarget.dataset.resource
    //console.log(resource);
    var resourceJson = JSON.stringify(resource);
    wx.navigateTo({
      url: '../forum/informationDetail/informationDetail?resource='+resourceJson,
    })
  },
  // 写回答
  write:function(e){
    var tag = e.currentTarget.dataset.tag
    wx.navigateTo({
      url: '/pages/write_answer/write_answer?tag='+tag,
    })
  },
  // ------------------页面切换、跳转模块 end-----------


  //根据标题搜索问题
  getQuestion:function(e){
    var question=e.detail.value
    this.setData({question:question})
  },

  searchQuestion:function(){
    var question=this.data.question
    var mark='searchQuestion';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method:'POST',
      data:{
        mark:mark,
        question:question
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var qList = res.data.data.questions;
        this.setData({
          questionList:qList,
        })
      }
    })
  },

   //根据标题搜索经验贴
   getArticle:function(e){
    var article=e.detail.value
    this.setData({article:article})
  },

  searchArticle:function(){
    var article=this.data.article
    var mark='searchArticle';
    wx.request({
      url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
      method:'POST',
      data:{
        mark:mark,
        article:article
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'Accept': 'application/json'
      },
      success: (res)=>{
        var aList = res.data.data.articles;
        this.setData({
          articleList:aList,
        })
      }
    })
  },

     //根据标题搜索资料
     getResource:function(e){
      var resource=e.detail.value
      this.setData({resource:resource})
    },
  
    searchResource:function(){
      var resource=this.data.resource
      var mark='searchResource';
      wx.request({
        url: 'http://localhost:8080/youYanZaiXian/ForumServlet',
        method:'POST',
        data:{
          mark:mark,
          resource:resource
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          'Accept': 'application/json'
        },
        success: (res)=>{
          var rList = res.data.data.resources;
          this.setData({
            resourceList:rList,
          })
        }
      })
    }

});

/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*/